package com.bingluo.ui;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityMain extends ListActivity {
	private List<Map<String, Object>> list;

	// private List<String> data = new ArrayList<String>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.main, new String[] { "music_title", "music_artist",
						"music_cover" }, new int[] { R.id.music_title,
						R.id.music_artist, R.id.music_cover });
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		List<Map<String, Object>> list_local = list;
		Map<String, Object> MusicInfo = list_local.get(position);
		String music_title = (String) MusicInfo.get("music_title");
		String music_artist = (String) MusicInfo.get("music_artist");
		String music_album = (String) MusicInfo.get("music_album");
		String music_year = (String) MusicInfo.get("music_year");
		String music_track = (String) MusicInfo.get("music_track");
		String music_genre = (String) MusicInfo.get("music_genre");
		String music_comment = (String) MusicInfo.get("music_comment");
		String music_album_artist = (String) MusicInfo
				.get("music_album_artist");
		String music_composer = (String) MusicInfo.get("music_composer");
		String music_discnumber = (String) MusicInfo.get("music_discnumber");
		int music_cover = R.drawable.cover;
		Resources myResources = getResources();
		InputStream is = myResources.openRawResource(music_cover);
		BitmapDrawable bmpDraw = new BitmapDrawable(is);
		Bitmap bmp = bmpDraw.getBitmap();
		// TextView music_title = (TextView) v.findViewById(R.id.music_title);
		Toast.makeText(this, music_artist, 1000).show();
		super.onListItemClick(l, v, position, id);
		// Intent it = new Intent(this, EditTag.class);

		Intent it = new Intent();
		// it.putExtra("music_title", music_title);
		// it.putExtra("music_artist", music_artist);
		// it.putExtra("music_album", music_album);
		// it.putExtra("music_year", music_year);
		// it.putExtra("music_track", music_track);
		// it.putExtra("music_genre", music_genre);
		// it.putExtra("music_comment", music_comment);
		// it.putExtra("music_album_artist", music_album_artist);
		// it.putExtra("music_composer", music_composer);
		// it.putExtra("music_discnumber", music_discnumber);
		// it.putExtra("music_cover", bmp);
		Bundle bundle = new Bundle();
		bundle.putString("key_height", music_title);
		bundle.putString("key_weight", music_artist);
		bundle.putString("music_title", music_title);
		bundle.putString("music_artist", music_artist);
		bundle.putString("music_album", music_album);
		bundle.putString("music_year", music_year);
		bundle.putString("music_track", music_track);
		bundle.putString("music_genre", music_genre);
		bundle.putString("music_comment", music_comment);
		bundle.putString("music_album_artist", music_album_artist);
		bundle.putString("music_composer", music_composer);
		bundle.putString("music_discnumber", music_discnumber);
		bundle.putParcelable("music_cover", bmp);
		it.setClass(this, EditTag.class);
		it.putExtras(bundle);
		startActivity(it);
	}

	public static byte[] getBytesFromInputStream(InputStream is, int bufsiz)
			throws IOException {
		int total = 0;
		byte[] bytes = new byte[4096];
		ByteBuffer bb = ByteBuffer.allocate(bufsiz);

		while (true) {
			int read = is.read(bytes);
			if (read == -1)
				break;
			bb.put(bytes, 0, read);
			total += read;
		}

		byte[] content = new byte[total];
		bb.flip();
		bb.get(content, 0, total);

		return content;
	}

	private List<Map<String, Object>> getData() {

		list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		// 歌名
		map.put("music_title", "法克这个人");
		// 演唱
		map.put("music_artist", "萧闳仁");
		// 专辑
		map.put("music_album", "法克这个人");
		// 年份
		map.put("music_year", "2009");
		// 音轨号
		map.put("music_track", "04");
		// 流派
		map.put("music_genre", "R&B");
		// 备注
		map.put("music_comment", "专辑主打歌");
		// 专辑艺人
		map.put("music_album_artist", "萧闳仁");
		// 作曲
		map.put("music_composer", "萧闳仁");
		// 碟号
		map.put("music_discnumber", "");
		// 封面
		map.put("music_cover", R.drawable.cover);
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("music_title", "看袂落去");
		map.put("music_artist", "萧闳仁");
		map.put("music_cover", R.drawable.cover);
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("music_title", "留声机");
		map.put("music_artist", "萧闳仁");
		map.put("music_cover", R.drawable.cover);
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("music_title", "王宝钏苦守寒窑十八年");
		map.put("music_artist", "萧闳仁");
		map.put("music_cover", R.drawable.cover);
		list.add(map);
		return list;
	}
}
